package com.app.micaihu.view.user.userinfo.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseListFragment;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.infor.AttentionAndFansBean;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.utils.InteractiveUtils;
import com.app.micaihu.view.user.userinfo.activity.MyHomepageDynamicActivity;
import com.app.micaihu.view.user.userinfo.adapter.AttentionAndFansAdapter;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 粉丝关注页面的fragment
 * Created by linkai on 2016/3/14.
 */
public class AttentionAndFansPageFragment extends BaseListFragment<AttentionAndFansBean> implements View.OnClickListener {

    private String toUid;
    private String attentionOrFans;

    protected String fragmentTitleName = "";

    /**
     * 获取 fragment的title
     *
     * @return
     */
    public String getFragmentTitleName() {
        if (TextUtils.isEmpty(fragmentTitleName)) {
            return "";
        }
        return fragmentTitleName;
    }

    public AttentionAndFansPageFragment() {

    }

    public static AttentionAndFansPageFragment newInstance(String attentionOrFans, String toUid) {
        AttentionAndFansPageFragment attentionAndFansPageFragment = new AttentionAndFansPageFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Config.Extra.PARAMETER1, attentionOrFans);
        bundle.putString(Config.Extra.PARAMETER2, toUid);
        attentionAndFansPageFragment.setArguments(bundle);
        return attentionAndFansPageFragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        attentionOrFans = getArguments().getString(Config.Extra.PARAMETER1);
        toUid = getArguments().getString(Config.Extra.PARAMETER2);
        if (UserInforManager.getInstance().isLogin()
                && TextUtils.equals(UserInforManager.getInstance().getUserInfor().getUid(), toUid)) {
            //自己的
            if (TextUtils.equals(attentionOrFans, "1")) {
                fragmentTitleName = "关注";
            } else if (TextUtils.equals(attentionOrFans, "2")) {
                fragmentTitleName = "粉丝";
            }
        } else {//其他人的
            if (TextUtils.equals(attentionOrFans, "1")) {
                fragmentTitleName = "TA的关注";
            } else if (TextUtils.equals(attentionOrFans, "2")) {
                fragmentTitleName = "TA的粉丝";
            }
        }

    }

    @Override
    protected void changeData(final boolean isRefresh) {
        Map<String, String> params = new HashMap<>();
        if (UserInforManager.getInstance().isLogin()) {
            params.put("uid", UserInforManager.getInstance().getUserInfor().getUid());
        } else {
            params.put("uid", "0");
        }

        params.put("toUid", toUid);
        params.put("type", attentionOrFans);
        params.put("page", dataPage + "");

        HttpListener<DataBean<List<AttentionAndFansBean>>> listListener = new HttpListener<DataBean<List<AttentionAndFansBean>>>() {
            @Override
            public void onStart() {
                super.onStart();
                setLoadView(LOADING, null);
            }

            @Override
            public void onError(VolleyError error) {
                setLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror_click));
            }

            @Override
            public void onSuccess(DataBean<List<AttentionAndFansBean>> response) {
                if (response.noError() && response.getData() != null) {
                    List<AttentionAndFansBean> attentionAndFansList = response.getData();
                    if (attentionAndFansList.size() > 0) {
                        if (dataList == null) {
                            dataList = new ArrayList<AttentionAndFansBean>();
                        }


                        for (int i = 0; i < attentionAndFansList.size(); i++) {
                            if (InteractiveUtils.getInstance().theDefriendIsContains(attentionAndFansList.get(i).getUid())) {
                                attentionAndFansList.remove(attentionAndFansList.get(i));
                                i--;
                            }
                        }


                        if (isRefresh) {
                            //清空数据
                            dataList.clear();
                        }
                        dataList.addAll(attentionAndFansList);

                        if (mAdapter == null) {
                            mAdapter = new AttentionAndFansAdapter(dataList, getActivity(), attentionOrFans, isOther());
                            mListView.setAdapter(mAdapter);
                        } else {
                            mAdapter.notifyDataSetChanged();
                        }
                        setLoadView(LOADSUCES, null);
                    } else {
                        if (isRefresh && dataList != null) {
                            dataList.clear();
                        }
                        if (dataList == null || dataList.size() == 0) {
                            if (mAdapter != null) mAdapter.notifyDataSetChanged();

                            if ("1".equals(attentionOrFans)) {
                                if (isOther()) {
                                    setLoadView(DATAEMPTY, TypeConstant.EMPTYVIEW_DEFAULT, getResources().getString(R.string.homepage_other_noattention));
                                } else {
                                    setLoadView(DATAEMPTY, TypeConstant.EMPTYVIEW_DEFAULT, getResources().getString(R.string.homepage_noattention));
                                }

                            } else if ("2".equals(attentionOrFans)) {
                                if (isOther()) {
                                    setLoadView(DATAEMPTY, TypeConstant.EMPTYVIEW_DEFAULT, getResources().getString(R.string.homepage_other_otnofans));
                                } else {
                                    setLoadView(DATAEMPTY, TypeConstant.EMPTYVIEW_DEFAULT, getResources().getString(R.string.homepage_nofans));
                                }

                            }
                        } else {
                            setLoadView(LOADSUCES, getString(R.string.homepage_msg_nomoremsg));
                        }
                    }

                } else {
                    setLoadView(DATAERROR, response.getNnderstoodMsg());
                }
            }
        };

        addPostCacheRequest(URIContant.ATTENTION_FANS, new TypeToken<DataBean<List<AttentionAndFansBean>>>() {
        }.getType(), params, listListener);

    }

    private boolean isOther() {
        if (!(UserInforManager.getInstance().isLogin()
                && TextUtils.equals(UserInforManager.getInstance().getUserInfor().getUid(), toUid))) {
            return true;
        }
        return false;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //因为有头部
        if (mListView.getRefreshableView().getHeaderViewsCount() > 0) {
            position = position - mListView.getRefreshableView().getHeaderViewsCount();
        }
        if (position >= dataList.size()) {
            position = dataList.size() - 1;
        } else if (position < 0) {
            position = 0;
        }
        AttentionAndFansBean attentionAndFansBean = dataList.get(position);
        if (attentionAndFansBean == null) return;
        String toUid = attentionAndFansBean.getUid();
        MyHomepageDynamicActivity.goToUserHomePage(activity, toUid);
    }


}
